home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4093 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in1.uu.net!allegra!alice!ark
  3. From: ark@research.att.com (Andrew Koenig)
  4. Subject: Re: SORTing problem c++
  5. Message-ID: <DLv3Bp.AHo@research.att.com>
  6. Organization: AT&T Research, Murray Hill NJ
  7. References: <310B0DD0.34F1@pi.net>
  8. Date: Sat, 27 Jan 1996 22:47:48 GMT
  9.  
  10. In article <310B0DD0.34F1@pi.net> heggie <heggie@pi.net> writes:
  11.  
  12. > what can I DO???
  13.  
  14. > #include <stdio.h>
  15. > #include <stdlib.h>
  16. > #include <string.h>
  17.  
  18. > int sort_function(char **a, char **b)
  19. > {
  20. >    return( strcmp(*a, *b));
  21. > }
  22.  
  23. > int main(int argc, char **argv)
  24. > {
  25. >    int
  26. >            x;
  27. >    qsort(argv, argc, sizeof(char*), sort_function);
  28. >    for (x = 0; x < argc; x++)
  29. >          printf("%s\n", argv[x]);
  30. >    return 0;
  31. > }
  32.  
  33. This is not a valid program either in C or C++.
  34. However, the typical C++ compiler will diagnose
  35. the error and the typical C compiler will not.
  36.  
  37. The problem is that the function passed to qsort
  38. is required to have parameters of type const void*
  39. and you gave it parameters of type char**.
  40.  
  41. So you need to rewrite the function this way (for example):
  42.  
  43.     int sort_function(const void* a, const void* b)
  44.     {
  45.         const char** aa = (const char**) a;
  46.         const char** bb = (const char**) b;
  47.         return strcmp(*aa, *bb);
  48.     }
  49.  
  50. That should do it.
  51. -- 
  52.                 --Andrew Koenig
  53.                   ark@research.att.com
  54.